﻿using log4net;
using StoreProject.BLL;
using StoreProject.BLL.SettingBLL;
using StoreProject.DAL.SettingDAL;
using StoreProject.Entity;
using StoreProject.Entity.SettingEntity;
using StoreProject.Enum;
using StoreProject.Forms.SettingForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreProject.Forms.WorkingProcedure
{
    public partial class WorkingProcedureForm : Form
    {
        ILog _log = LogManager.GetLogger("WorkingProcedureForm");
        WorkingProcedureBLL workingProcedureBLL = new WorkingProcedureBLL();
        WorkingProcedureNameBLL workingProcedureNameBLL = new WorkingProcedureNameBLL();
        WorkingProcedureRemarkBLL workingProcedureRemarkBLL = new WorkingProcedureRemarkBLL();
        protected FormState _formState = FormState.INIT;
        BindingList<WorkingProcedureEntity> workingProcedureList = null;
        string processFlowId = null;
        protected object _currentObj = null;
        public object CurrEntity
        {
            get { return _currentObj; }
            set { _currentObj = value; }
        }

        private bool CanUpdate()
        {
            if (_currentObj == null)
                return false;
            return true;
        }
        public WorkingProcedureForm(FormState state,List<WorkingProcedureEntity> list)
        {
            if (list != null && list.Count > 0)
            {
                workingProcedureList = new BindingList<WorkingProcedureEntity>(list.OrderBy(x=>x.IndexNum).ToList());
                processFlowId = list[0].ProcessFlowId;
            }
            _formState = state;
            InitializeComponent();
        }

        

        public WorkingProcedureForm(FormState state)
        {
            workingProcedureList = new BindingList<WorkingProcedureEntity>();
            processFlowId = Guid.NewGuid().ToString();
            InitializeComponent();
            _formState = state;
        }

        
        private void btnAddWorkingProcedure_Click(object sender, EventArgs e)
        {
            var entity = GetEntiy();
            var exist = workingProcedureList.Where(x => x.IndexNum == entity.IndexNum).ToList();
            if (exist != null && exist.Count > 0)
            {
                MessageBox.Show("该工序顺序已经存在，不可重复添加。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            workingProcedureList.Add(entity);
            numIndex.Value = Convert.ToInt32(numIndex.Value) + 1;
            dataGridView1.DataSource = workingProcedureList;
        }

        public WorkingProcedureEntity GetEntiy()
        {
            WorkingProcedureEntity entity = new WorkingProcedureEntity();
            entity.Id = Guid.NewGuid().ToString();
            entity.ProcessFlowId = processFlowId;
            entity.Remark = cmbRemark.Text;
            entity.CreateTime = DateTime.Now;
            entity.UpdateTime = DateTime.Now;
            entity.IndexName = cmbName.Text;
            entity.IndexNum = Convert.ToInt32(numIndex.Value);
            entity.IsDeleted = false;
            return entity;
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            if (dgv.CurrentRow != null)
            {
                this._currentObj = dgv.CurrentRow.DataBoundItem;
            }
            else
            {
                this._currentObj = null;
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WorkingProcedureEntity workingProcedureEntity = this._currentObj as WorkingProcedureEntity;
            if (CanUpdate() && !string.IsNullOrEmpty(workingProcedureEntity.Id))
            {
                WorkingProcedureSettingForm workingProcedureSettingForm = new WorkingProcedureSettingForm(FormState.UPDATE, workingProcedureEntity);
                workingProcedureSettingForm.TopMost = true;
                workingProcedureSettingForm.ShowDialog();
                workingProcedureSettingForm.BringToFront();
            }
        }

        private void WorkingProcedureForm_Load(object sender, EventArgs e)
        {
            cmbName.DisplayMember = "WorkingProcedureName";
            cmbName.ValueMember = "WorkingProcedureName";
            cmbRemark.DisplayMember = "WorkingProcedureRemark";
            cmbRemark.ValueMember = "WorkingProcedureRemark";
            
            InitData();
        }
        private void InitData()
        {
            if (_formState == FormState.ADD)
            {
                this.Text = "新增工序";
            }
            else if (_formState == FormState.UPDATE)
            {
                this.Text = "修改工序";
            }
            if (workingProcedureList != null && workingProcedureList.Count > 0)
            {
                dataGridView1.DataSource = workingProcedureList;
                numIndex.Value = workingProcedureList.Select(x=>x.IndexNum).Max() + 1;
            }

            
            var workingProcedureName = workingProcedureNameBLL.GetList().ToList();
            cmbName.DataSource = workingProcedureName;

            var workingProcedureRemark = workingProcedureRemarkBLL.GetList().ToList();
            cmbRemark.DataSource = workingProcedureRemark;
        }
        private void numIndex_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
                e.Handled = true;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (workingProcedureList != null && workingProcedureList.Count > 0)
            {
                foreach (var item in workingProcedureList.ToList())
                {
                    var isExist = workingProcedureBLL.Get(item.Id);
                    if (isExist != null)
                    {
                        workingProcedureBLL.Update(item);
                    }
                    else
                    {
                        workingProcedureBLL.Insert(item);
                    }
                }
                MessageBox.Show("保存成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
                MessageBox.Show("没有需要保存的数控！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            WorkingProcedureEntity workingProcedureEntity = this._currentObj as WorkingProcedureEntity;
            if (CanUpdate() && !string.IsNullOrEmpty(workingProcedureEntity.Id))
            {
                workingProcedureList.Remove(workingProcedureEntity);
            }
        }
    }
}
